/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyClass;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 *
 * @author HuuTai_it
 */
public class GameSave {
    
    private RecordStore rStore;
    private Control control;
    
    public GameSave(Control control) {
        
        this.control = control;
        
        openRS();        
        closeRS();
    }
    
    public void openRS(){
        
        try {rStore = RecordStore.openRecordStore("SaveGame", true);}
        catch(RecordStoreException ex){}
    }

    public void closeRS(){
        
        try{rStore.closeRecordStore();}
        catch (RecordStoreNotOpenException ex){}
        catch (RecordStoreException ex){}
    }
    
    public void SaveGame () throws Exception {
        
        openRS(); 
        
        try {
            if(rStore.getNumRecords() == 0)  {

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream(baos);
            
                dos.writeInt(control.level);
                dos.writeInt(control.difficulties);
                dos.writeInt(control.life);
                dos.writeInt(control.scores);
                dos.writeBoolean(control.activeSound);

                rStore.addRecord(baos.toByteArray(), 0, baos.toByteArray().length);
            }
            else {         
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream(baos);

                dos.writeInt(control.level);
                dos.writeInt(control.difficulties);
                dos.writeInt(control.life);
                dos.writeInt(control.scores);
                dos.writeBoolean(control.activeSound);

                rStore.setRecord(1, baos.toByteArray(), 0, baos.toByteArray().length);                
            }
        }
        catch(RecordStoreNotFoundException rsnfe){}
        catch(RecordStoreException rse){}
        catch(Exception ioe){}
        
        closeRS();
    }
    
    public boolean LoadGame() throws Exception {
        
        openRS();
        
        if(rStore.getNumRecords() > 0)  {
            byte[] b = null;

            try{

                    b = rStore.getRecord(1);
                    ByteArrayInputStream bin =  new ByteArrayInputStream( b );

                    DataInputStream din = new DataInputStream( bin );

                    control.level = din.readInt();
                    control.difficulties = din.readInt();
                    control.life = din.readInt();
                    control.scores = din.readInt();
                    control.activeSound = din.readBoolean();

                    bin.reset();
                    din.close();
                    din = null;
            }
            catch(RecordStoreNotFoundException rsnfe){}
            catch (IOException e){}
            catch(RecordStoreException e){}
            closeRS();
            return true;
        }
        
        closeRS();
        return false;
    }    
    
}